package com.party.service.study.impl;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.mapper.AnswerMapper;
import com.party.mapper.TestMapper;
import com.party.model.Answer;
import com.party.model.AnswerExample;
import com.party.model.Test;
import com.party.model.TestExample;
import com.party.service.study.AnswerService;
import com.party.service.study.TestService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class TestServiceImpl implements TestService
{
    @Resource
    TestMapper mapper;
    @Resource
    AnswerMapper answerMapper;
    @Resource
    AnswerService answerService;
    @Override
    public int add(Test test) {
        if(test != null)
        {
            return this.mapper.insert(test);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if(id != null && id > 0)
        {
            //删除答案
            AnswerExample example = new AnswerExample();
            AnswerExample.Criteria criteria = example.createCriteria();
            criteria.andTestidEqualTo(id);
            this.answerMapper.deleteByExample(example);

            return this.mapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public Test getById(Integer id) {
        if(id != null && id > 0)
        {
            return this.mapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public List<Test> getByIds(List<Integer> ids) {
        if(ids != null && ids.size() > 0)
        {
            TestExample example = new TestExample();
            TestExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(ids);
            return this.mapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<Test> getByContentId(Integer id) {
        if(id != null && id > 0)
        {
            TestExample example = new TestExample();
            TestExample.Criteria criteria = example.createCriteria();
            criteria.andContentidEqualTo(id);
            example.setOrderByClause("id desc");
            List<Test> testList = this.mapper.selectByExample(example);
            if(testList != null && testList.size()>0)
            {
                //查找到题目下的答案
                for (Test test:testList)
                {
                    List<Answer> answers = this.answerService.getByTestId(test.getId());
                    if(answers != null && answers.size() >0)
                    {
                        test.setAnswerList(answers);
                    }
                }
            }
            return testList;
        }
        return null;
    }

    @Override
    public Page getList(Test test, int pageSize, int pageNumber) {
        //获取总的记录数量
        int count = this.mapper.countByPage(test);
        //获取指定的页码数据
        test.setBeginNum((pageNumber - 1) * pageSize);
        test.setEndNum(pageSize);
        List<Test> list = this.mapper.getListPage(test);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;

    }

    @Override
    public int addTestAndAnswer(Test test, List<Answer> listExamAnswer) {
        int num = this.mapper.insertSelective(test);
        if(test.getId() != null && test.getId() > 0)
        {
            for(Answer examAnswer:listExamAnswer){
                examAnswer.setTestid(test.getId());
                answerMapper.insertSelective(examAnswer);
            }
        }
        return num;
    }

    @Override
    public int editTestAndAnswer(Test test, List<Answer> list) {
        int num=mapper.updateByPrimaryKeySelective(test);
        //首先删除原来的所有答案，然后在插入
        AnswerExample example = new AnswerExample();
        AnswerExample.Criteria criteria = example.createCriteria();
        criteria.andTestidEqualTo(test.getId());
        answerMapper.deleteByExample(example);

        for(Answer examAnswer:list){
            examAnswer.setTestid(test.getId());
            answerMapper.insertSelective(examAnswer);
        }
        return num;
    }

    @Override
    public int delete(String ids) {
        if(ids != null && !"".equals(ids))
        {
            String[] idStr = ids.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for(String id: idStr)
            {
                if(Tools.isNumber(id))
                {
                    idList.add(Integer.parseInt(id));
                    //删除答案
                    AnswerExample example = new AnswerExample();
                    AnswerExample.Criteria criteria = example.createCriteria();
                    criteria.andTestidEqualTo(Integer.parseInt(id));
                    this.answerMapper.deleteByExample(example);
                }
            }
            if(idList.size() > 0)
            {
                TestExample example = new TestExample();
                TestExample.Criteria criteria = example.createCriteria();
                criteria.andIdIn(idList);
                return this.mapper.deleteByExample(example);
            }
        }
        return 0;
    }
}
